//
//  SMAuthenBaseView.swift
//  SomePasti
//
//  Created by 李永彬 on 2025/10/17.
//

import SwiftUI

struct SMAuthenBaseView<Content: View>: View {

    let content: Content
    var baseConfig: SMBaseConfig
    var navigationConfig: NavigationConfig
    
    @Environment(\.presentationMode) var presentationMode
    @State private var showCustomPopup: Bool = false
    @Environment(\.navigationManager) var navManager
    
    init(baseConfig: SMBaseConfig = SMBaseConfig(),
         navigationConfig: NavigationConfig = NavigationConfig(title: ""),
         @ViewBuilder content: () -> Content) {
        self.baseConfig = baseConfig
        self.navigationConfig = navigationConfig
        self.content = content()
    }
    
    var body: some View {
        
        let buttons: [SMPopupButton] = [
            SMPopupButton.init("Cancel", style: .cancel) {
                showCustomPopup = false
            },
            SMPopupButton.init("确认") {
                showCustomPopup = false
//                presentationMode.wrappedValue.dismiss()

                navManager.wrappedValue.popToView(tag: mainNav, viewType: SMProductDetailView.self, animated: true)
            }
        ]
        
        SMBaseContentView(navigationConfig: navigationConfig) {
            ZStack {
                content
            }
            .toolbar {
                NavigationToolbarContent(
                    showBackButton: true,
                    backButtonColor: Color(hex: "#000000"),
                    image: Image(systemName: "chevron.left"),
                    onBack: closeView
                )
            }
            .smAlertOnWindow(isPresented: $showCustomPopup, title: "确认退出吗",
                             message: "please give me some time.",
                             backgroundImage: "alert_bg_image1",
                             buttons: buttons)
        }

    }
    
    private func closeView() {
        showCustomPopup = true
    }
}


